package com.ooono.factorytest;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ooono.factorytest.adapter.TestItemAdapter;
import com.ooono.factorytest.data.TestItemManager;
import com.ooono.factorytest.model.TestItem;
import com.ooono.factorytest.utils.AppUtils;
import com.ooono.factorytest.utils.SPUtils;
import com.ooono.factorytest.view.RecycleGridDivider;

import java.util.List;

import mcs8.mdm.PolicyManager;

public class MainActivity extends AppCompatActivity implements TestItemAdapter.OnItemClickListener {

    private static final String TAG = "MainActivity";

    RecyclerView mItemsRv;
    TestItemAdapter mTestItemAdapter;
    TextView mVersionTv;
    Button mCleanResultBtn;

    private List<TestItem> mTestItems;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        mItemsRv = findViewById(R.id.rv_items);
        mVersionTv = findViewById(R.id.tv_version);
        mCleanResultBtn = findViewById(R.id.btn_clean_result);
        mCleanResultBtn.setOnClickListener(v -> {
            SPUtils.get(this).clear();
            mTestItems = TestItemManager.getInstance(getApplicationContext()).loadTestItemsActivity();
            mTestItemAdapter.notifyDataSetChanged();
        });

        // 保持屏幕常亮
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        mVersionTv.setText(AppUtils.getVersionName(this));

        mTestItems = TestItemManager.getInstance(getApplicationContext()).loadTestItemsActivity();
        mTestItemAdapter = new TestItemAdapter(this, mTestItems);
        mTestItemAdapter.setOnItemClickListener(this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 4);
        mItemsRv.setLayoutManager(layoutManager);
        mItemsRv.addItemDecoration(new RecycleGridDivider(this));
        mItemsRv.setAdapter(mTestItemAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onResume() {
        super.onResume();
        mTestItems = TestItemManager.getInstance(getApplicationContext()).loadTestItemsActivity();
        mTestItemAdapter.notifyDataSetChanged();
        PolicyManager.setNotificationBarEnable(false);
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(MainActivity.this, mTestItems.get(position).getActivity());
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        PolicyManager.setNotificationBarEnable(true);
        super.onPause();
    }


}